package com.example.myapplicationndk.utils;


import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.codec.binary.Base64;

import java.net.URLEncoder;
import java.util.Objects;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class DingDingUtils {

    private static final String DINDING_URL = "你自己的 webhook ";
    private static final String DINDING_SECRET = "你自己的 秘钥 ";


    private static String getCompliteUrl() throws Exception {
        Long timestamp = System.currentTimeMillis();
        String secret = DINDING_SECRET;

        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");

        return DINDING_URL + "&timestamp=" + timestamp + "&sign=" + sign;
    }


    private static void send(String textMsg) {
        try {
            OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");//数据类型为json格式，
            RequestBody body = RequestBody.create(JSON, textMsg);
            Request request = new Request.Builder().url(getCompliteUrl()).post(body).build();
            Response response = client.newCall(request).execute();

            if (response.code() == 200 && !(response.body() == null)) {
                String result = Objects.requireNonNull(response.body()).string();
                Log.e("TAG", "发送钉钉成功：" + result);
            } else {
                Log.e("TAG", "发送钉钉消息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //发送text格式的消息
    public static void sendText(String mag, String[] atMobile, boolean isAtAll) throws Exception {
        JSONObject object = new JSONObject();
        JSONObject object_text = new JSONObject();
        JSONObject object_at = new JSONObject();

        object_text.put("content", mag);
        object_at.put("atMobiles", atMobile);
        object_at.put("isAtAll", isAtAll);

        object.put("msgtype", "text");
        object.put("text", object_text);
        object.put("at", object_at);

        send(String.valueOf(object));
    }


    //发送markdown格式的消息
    public static void sendMarkdown(String title, String text, String[] atMobile, boolean isAtAll) throws Exception {
        JSONObject object = new JSONObject();
        JSONObject object_markdown = new JSONObject();
        JSONObject object_at = new JSONObject();

        object_markdown.put("title", title);
        object_markdown.put("text", text);
        object_at.put("atMobiles", atMobile);
        object_at.put("isAtAll", isAtAll);

        object.put("msgtype", "markdown");
        object.put("markdown", object_markdown);
        object.put("at", object_at);

        send(String.valueOf(object));
    }

    //整体跳转ActionCard类型
    public static void sendActionCard(String title, String text, String singleTitle, String singleURL, int btnOrientation, int hideAvatar) throws Exception {
        JSONObject object = new JSONObject();
        JSONObject object_actionCard = new JSONObject();

        object_actionCard.put("title", title);
        object_actionCard.put("text", text);
        object_actionCard.put("hideAvatar", hideAvatar);
        object_actionCard.put("btnOrientation", btnOrientation);
        object_actionCard.put("singleTitle", singleTitle);
        object_actionCard.put("singleURL", singleURL);

        object.put("actionCard", object_actionCard);
        object.put("msgtype", "actionCard");

        send(String.valueOf(object));
    }

    //独立跳转ActionCard类型
    public void sendActionCard1(String title, String text, String btnsTitle1, String btnsTitle2, String actionURL1, String actionURL2, int btnOrientation) throws Exception {
        JSONObject object = new JSONObject();
        JSONObject object_actionCard1 = new JSONObject();
        JSONObject object_btns1 = new JSONObject();
        JSONObject object_btns2 = new JSONObject();

        object_btns1.put("title", btnsTitle1);
        object_btns1.put("actionURL", actionURL1);
        object_btns2.put("title", btnsTitle2);
        object_btns2.put("actionURL", actionURL2);

        object_actionCard1.put("title", title);
        object_actionCard1.put("text", text);
        object_actionCard1.put("btnOrientation", btnOrientation);

        JSONArray btns = new JSONArray();
        btns.add(object_btns1);
        btns.add(object_btns2);
        object_actionCard1.put("btns", btns);

        object.put("actionCard", object_actionCard1);
        object.put("msgtype", "actionCard");

        send(String.valueOf(object));
    }

    //FeedCard类型
    public static void sendFeedCard(String title1, String messageURL1, String picURL1, String title2, String messageURL2, String picURL2) throws Exception {
        JSONObject object = new JSONObject();
        JSONObject object_feedCard = new JSONObject();
        JSONObject object_link1 = new JSONObject();
        JSONObject object_link2 = new JSONObject();

        object_link1.put("title", title1);
        object_link1.put("messageURL", messageURL1);
        object_link1.put("picURL", picURL1);

        object_link2.put("title", title2);
        object_link2.put("messageURL", messageURL2);
        object_link2.put("picURL", picURL2);

        JSONArray links = new JSONArray();
        links.add(object_link1);
        links.add(object_link2);
        object_feedCard.put("links", links);

        object.put("feedCard", object_feedCard);
        object.put("msgtype", "feedCard");

        send(String.valueOf(object));
    }
}

